package com.xunk.common.core.utils;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.soap.MimeHeaders;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.common.core.utils
 * @ClassName: RequestUtils
 * @Author: luanzhiwei
 * @Description:
 * @Date: 2021/4/21 14:07
 * @Version: 1.0
 */
@NoArgsConstructor
@Slf4j
public class RequestUtils {

    public static void setParam2Header(HttpServletRequest request, String key, String value) {
        Class<? extends HttpServletRequest> requestClass = request.getClass();
        try {
            Field request1 = requestClass.getDeclaredField("request");
            request1.setAccessible(true);
            Object o = request1.get(request);
            Field coyoteRequest = o.getClass().getDeclaredField("coyoteRequest");
            coyoteRequest.setAccessible(true);
            Object o1 = coyoteRequest.get(o);
            Field headers = o1.getClass().getDeclaredField("headers");
            headers.setAccessible(true);
            MimeHeaders o2 = (MimeHeaders) headers.get(o1);
            o2.setHeader(key, value);
        } catch (Exception e) {
            log.error("设置请求头失败", e);
        }
    }

}
